<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head th:replace="fragments/head :: commonHead('我的响应计划')">
    <!-- 页面特定样式将由通用头部替代 -->
</head>
<body>
    <div class="container mt-4">
        <div class="row mb-3">
            <div class="col">
                <h2>我的响应计划</h2>
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a th:href="@{/}" class="text-decoration-none">首页</a></li>
                        <li class="breadcrumb-item active" aria-current="page">我的响应计划</li>
                    </ol>
                </nav>
            </div>
        </div>
        
        <div class="row mb-3">
            <div class="col">
                <div class="card">
                    <div class="card-header d-flex justify-content-between align-items-center">
                        <h5 class="mb-0">响应计划列表</h5>
                    </div>
                    <div class="card-body">
                        <div class="table-responsive">
                            <table class="table table-hover">
                                <thead>
                                    <tr>
                                        <th>#</th>
                                        <th>计划名称</th>
                                        <th>相关风险</th>
                                        <th>计划开始时间</th>
                                        <th>计划结束时间</th>
                                        <th>状态</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr th:if="${responses.empty}">
                                        <td colspan="7" class="text-center">暂无响应计划</td>
                                    </tr>
                                    <tr th:each="response, stat : ${responses}">
                                        <td th:text="${stat.count}"></td>
                                        <td th:text="${response.name}"></td>
                                        <td th:text="${response.riskTitle}"></td>
                                        <td th:text="${#dates.format(response.expectedStartTime, 'yyyy-MM-dd HH:mm')}"></td>
                                        <td th:text="${#dates.format(response.expectedEndTime, 'yyyy-MM-dd HH:mm')}"></td>
                                        <td>
                                            <span th:if="${response.status == 1}" class="badge badge-secondary">未开始</span>
                                            <span th:if="${response.status == 2}" class="badge badge-primary">进行中</span>
                                            <span th:if="${response.status == 3}" class="badge badge-success">已完成</span>
                                            <span th:if="${response.status == 4}" class="badge badge-danger">已逾期</span>
                                        </td>
                                        <td>
                                            <div class="btn-group btn-group-sm">
                                                <a th:href="@{/risk-response/{id}(id=${response.id})}" class="btn btn-info" title="详情">
                                                    <i class="fa fa-eye"></i>
                                                </a>
                                                <button type="button" class="btn btn-success" th:onclick="'markAsCompleted(' + ${response.id} + ')'" th:disabled="${response.status == 3}" title="标记为已完成">
                                                    <i class="fa fa-check"></i>
                                                </button>
                                                <button type="button" class="btn btn-primary" th:onclick="'markAsInProgress(' + ${response.id} + ')'" th:disabled="${response.status == 2}" title="标记为进行中">
                                                    <i class="fa fa-play"></i>
                                                </button>
                                            </div>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="row">
            <div class="col">
                <a th:href="@{/dashboard}" class="btn btn-secondary">
                    <i class="fa fa-arrow-left"></i> 返回首页
                </a>
            </div>
        </div>
    </div>
    
    <!-- 完成时间输入模态框 -->
    <div class="modal fade" id="completionModal" tabindex="-1" role="dialog" aria-labelledby="completionModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="completionModalLabel">标记为已完成</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        <label for="actualEndTime">实际完成时间:</label>
                        <input type="datetime-local" class="form-control" id="actualEndTime">
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                    <button type="button" id="confirmCompletionBtn" class="btn btn-success">确认</button>
                </div>
            </div>
        </div>
    </div>
    
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.bundle.min.js"></script>
    <script th:inline="javascript">
        let responseIdToUpdate = null;
        
        // 标记为已完成
        function markAsCompleted(responseId) {
            responseIdToUpdate = responseId;
            
            // 设置当前时间为默认完成时间
            const now = new Date();
            const formattedNow = now.toISOString().slice(0, 16);
            $('#actualEndTime').val(formattedNow);
            
            $('#completionModal').modal('show');
        }
        
        // 标记为进行中
        function markAsInProgress(responseId) {
            if (confirm('确定将此响应计划标记为进行中？')) {
                updateResponseStatus(responseId, 2);
            }
        }
        
        $(document).ready(function() {
            // 确认完成按钮点击事件
            $('#confirmCompletionBtn').click(function() {
                if (responseIdToUpdate) {
                    const actualEndTime = $('#actualEndTime').val();
                    if (!actualEndTime) {
                        alert('请选择实际完成时间');
                        return;
                    }
                    
                    // 发送更新实际完成时间的请求
                    $.ajax({
                        url: '/risk-management/risk-response/actual-end-time/' + responseIdToUpdate,
                        type: 'POST',
                        data: { actualEndTime: new Date(actualEndTime).toISOString() },
                        success: function(response) {
                            if (response.code === 200) {
                                // 关闭模态框
                                $('#completionModal').modal('hide');
                                // 重新加载页面
                                location.reload();
                            } else {
                                alert(response.message || '更新失败');
                            }
                        },
                        error: function() {
                            alert('系统错误，请稍后再试');
                        }
                    });
                }
            });
        });
        
        // 更新响应计划状态
        function updateResponseStatus(responseId, status) {
            $.ajax({
                url: '/risk-management/risk-response/status/' + responseId + '/' + status,
                type: 'POST',
                success: function(response) {
                    if (response.code === 200) {
                        // 重新加载页面
                        location.reload();
                    } else {
                        alert(response.message || '更新状态失败');
                    }
                },
                error: function() {
                    alert('系统错误，请稍后再试');
                }
            });
        }
    </script>
</body>
</html> 